package com.clear.subject.domain.handler.subject;

import com.alibaba.fastjson.JSON;
import com.clear.subject.common.enums.IsDeletedFlagEnum;
import com.clear.subject.common.enums.SubjectInfoTypeEnum;
import com.clear.subject.domain.convert.SubjectJudgeConverter;
import com.clear.subject.domain.convert.SubjectMultipleConverter;
import com.clear.subject.domain.entity.SubjectAnswerBO;
import com.clear.subject.domain.entity.SubjectInfoBO;
import com.clear.subject.domain.entity.SubjectOptionBO;
import com.clear.subject.infra.basic.entity.SubjectJudge;
import com.clear.subject.infra.basic.entity.SubjectMultiple;
import com.clear.subject.infra.basic.service.SubjectMultipleService;
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * 多选题目的策略类
 */
@Component
@Slf4j
public class MultipleTypeHandler implements SubjectTypeHandler {

    @Resource
    private SubjectMultipleService subjectMultipleService;

    /**
     * 枚举身份的识别
     *
     * @return
     */
    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        return SubjectInfoTypeEnum.MULTIPLE;
    }

    /**
     * 实际的题目的插入
     *
     * @param subjectInfoBO
     */
    @Override
    public void add(SubjectInfoBO subjectInfoBO) {
        // ====多选题目的插入=====
        // 注意：多选题目的插入其实与单选题目插入的流程是一样的，唯一不同的是 答案选择中 is_correct 字段的值
        try {
            if (log.isInfoEnabled()) {
                log.info("MultipleTypeHandler.add.bo:{}", JSON.toJSONString(subjectInfoBO));
            }
            Preconditions.checkArgument(!CollectionUtils.isEmpty(subjectInfoBO.getOptionList()), "题目选项不能为空");

            List<SubjectMultiple> subjectMultipleList = new LinkedList<>();   // 多选题的选项有好多个，创建一个容器，进行批量插入

            subjectInfoBO.getOptionList().forEach(option -> {   // 遍历所有选项，并add到容器中
                SubjectMultiple multiple = SubjectMultipleConverter.INSTANCE.convertBOToEntity(option);
                multiple.setSubjectId(subjectInfoBO.getId()); // 为每个选择设置 subject_id
                multiple.setIsDeleted(IsDeletedFlagEnum.UN_DELETE.getCode());

                subjectMultipleList.add(multiple);
            });

            subjectMultipleService.batchInsert(subjectMultipleList);
        } catch (Exception e) {
            log.error("MultipleTypeHandler.add.error:{}", e.getMessage(), e);
        }

    }

    /**
     * 查询题目详情
     *
     * @param subjectId 题目id
     */
    @Override
    public SubjectOptionBO query(int subjectId) {
        SubjectMultiple subjectMultiple = new SubjectMultiple();
        subjectMultiple.setSubjectId(Long.valueOf(subjectId));

        // 根据题目id去查
        List<SubjectMultiple> result = subjectMultipleService.queryByCondition(subjectMultiple);

        List<SubjectAnswerBO> subjectAnswerBOList = SubjectMultipleConverter.INSTANCE.convertEntityToBoList(result);

        SubjectOptionBO subjectOptionBO = new SubjectOptionBO();
        subjectOptionBO.setOptionList(subjectAnswerBOList);

        return subjectOptionBO;
    }
}
